﻿using DatabaseService;
using DatabaseService.DatabaseDAO;
using DatabaseService.DatabaseService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace You.Respository
{
    /// <summary>
    /// 抽象仓储
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseRespository<T> : IRespository<T> where T : TableModelMapper, new()
    {
        protected IDatabaseDAO databaseServiceDAO = new DatabaseServiceDAO();

        /// <summary>
        /// 类名简称
        /// </summary>
        protected string modelName
        {
            get
            {
                Type type = typeof(T);
                return type.Name.Replace("Mapper", "");
            }
        }

        public BaseRespository()
        {
            //可用依赖注入框架注入
            databaseServiceDAO.databaseService = new SqliteDatabaseService();
            databaseServiceDAO.databaseService.databaseInfoDAO = new SqliteDatabaseInfoDAO();
        }

        /// <summary>
        /// 通过Id删除
        /// 需要新建id="DeleteById"的sql
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(T entity)
        {
            if (entity == null) return false;

            MapperDictionary parameters = new MapperDictionary { { "id", entity.Id } };
            DaoParametersEntity daoEntity = new DaoParametersEntity("DeleteById", parameters);
            return databaseServiceDAO.Delete<T>(daoEntity) > 0;
        }

        /// <summary>
        /// 修改元素，需要子类具体实现
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Update(T entity)
        {
            if (entity == null) return false;

            //获取Type对象，反射操作基本都是用Type进行的
            Type type = typeof(T);
            MapperDictionary parameters = new MapperDictionary();
            //获取Type对象所有公共属性
            PropertyInfo[] propertys = type.GetProperties();
            foreach (PropertyInfo property in propertys)
            {
                if (property.Name.ToLower() == "id") continue;
                var value = property.GetValue(entity);
                parameters.Add(property.Name.ToLower(), value);
            }
            parameters.Add("id", entity.Id);

            DaoParametersEntity daoEntity = new DaoParametersEntity($"Update{modelName}", parameters);
            return databaseServiceDAO.Update<T>(daoEntity) > 0;
        }

        /// <summary>
        /// 通过Id获取第一个元素
        /// 需要新建id="SelectById"的sql
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual T GetById(int id)
        {
            MapperDictionary parameters = new MapperDictionary { { "id", id } };
            DaoParametersEntity entity = new DaoParametersEntity("SelectById", parameters);
            return databaseServiceDAO.Select<T>(entity).FirstOrDefault();
        }

        /// <summary>
        /// 获取全部数据
        /// 需要新建id="GetList"的sql
        /// </summary>
        /// <returns></returns>
        public virtual IList<T> GetList()
        {
            //databaseServiceDAO.CheckDBconn();
            DaoParametersEntity entity = new DaoParametersEntity("GetList");
            return databaseServiceDAO.Select<T>(entity);
        }

        /// <summary>
        /// 新增元素
        /// 需要子类具体实现
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Insert(T entity)
        {
            if (entity == null) return false;

            //获取Type对象，反射操作基本都是用Type进行的
            Type type = typeof(T);
            MapperDictionary parameters = new MapperDictionary();
            //获取Type对象所有公共属性
            PropertyInfo[] propertys = type.GetProperties();
            foreach (PropertyInfo property in propertys)
            {
                if (property.Name.ToLower() == "id") continue;
                var value = property.GetValue(entity);
                parameters.Add(property.Name.ToLower(), value);
            }

            DaoParametersEntity daoEntity = new DaoParametersEntity($"Insert{modelName}", parameters);
            return databaseServiceDAO.Insert<T>(daoEntity) > 0;
        }

        /// <summary>
        /// 主键={id}的数据是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool IsExistById(int id)
        {
            return GetById(id) == null;
        }
    }
}
